//
//  XKCertificationView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/19.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKCertificationView.h"


@interface XKCertificationView () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *backgroundScrollView; // 主背景滚动图

// 白背景
@property (nonatomic, strong) UIView *backgroundView_name;      // 姓名
@property (nonatomic, strong) UIView *backgroundView_gender;    // 性别
@property (nonatomic, strong) UIView *backgroundView_nation;    // 民族
@property (nonatomic, strong) UIView *backgroundView_birthday;  // 生日
@property (nonatomic, strong) UIView *backgroundView_address;   // 地址
@property (nonatomic, strong) UIView *backgroundView_IDNumber;  // 身份证号
@property (nonatomic, strong) UIView *backgroundView_tradePassword; // 交易密码
@property (nonatomic, strong) UIView *backgroundView_tradePasswordConfirm; // 确认密码

// 提示语
@property (nonatomic, strong) UILabel *label_tip;

// 提示标题
@property (nonatomic, strong) UILabel *titleLabel_name;
@property (nonatomic, strong) UILabel *titleLabel_gender;    // 性别
@property (nonatomic, strong) UILabel *titleLabel_nation;    // 民族
@property (nonatomic, strong) UILabel *titleLabel_birthday;  // 生日
@property (nonatomic, strong) UILabel *titleLabel_address;   // 地址
@property (nonatomic, strong) UILabel *titleLabel_IDNumber;
@property (nonatomic, strong) UILabel *titleLabel_tradePassword;
@property (nonatomic, strong) UILabel *titleLabel_tradePasswordConfirm;

// 输入框
@property (nonatomic, strong) UITextField *textField_name;
@property (nonatomic, strong) UITextField *textField_gender;    // 性别
@property (nonatomic, strong) UITextField *textField_nation;    // 民族
@property (nonatomic, strong) UITextField *textField_birthday;  // 生日
@property (nonatomic, strong) UITextField *textField_address;   // 地址
@property (nonatomic, strong) UITextField *textField_IDNumber;
@property (nonatomic, strong) UITextField *textField_tradePassword;
@property (nonatomic, strong) UITextField *textField_tradePasswordConfirm;

// 确认按钮
@property (nonatomic, strong) UIButton *button_confirm;

@end

@implementation XKCertificationView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}


#pragma mark - 提交实名信息
- (void)submitInfo:(UIButton *)sender {
    if (self.SubmitInfoBlock) {
        self.SubmitInfoBlock(self.textField_name.text, self.textField_IDNumber.text, self.textField_tradePasswordConfirm.text);
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField becomeFirstResponder];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (self.textField_IDNumber.text.length >= 16 &&
        self.textField_tradePassword.text.length == 6 &&
        self.textField_tradePasswordConfirm.text.length == 6) {
        
        self.button_confirm.enabled = YES;
    } else {
        self.button_confirm.enabled = NO;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == self.textField_IDNumber) {
        return text.length > 18 ? NO : YES;
    } else if (textField == self.textField_tradePassword || textField == self.textField_tradePasswordConfirm) {
        return text.length > 6 ? NO : YES;
    }
    
    return YES;
}


- (void)createSubviews {
    
    self.backgroundScrollView = [[UIScrollView alloc] init];
    self.backgroundScrollView.contentSize = CGSizeMake(Main_Width, Main_Height);
    
    
    self.backgroundView_name = [UIView new];
    self.backgroundView_name.backgroundColor = WhiteColor;
    
    self.backgroundView_gender = [UIView new];
    self.backgroundView_gender.backgroundColor = WhiteColor;
    
    self.backgroundView_nation = [UIView new];
    self.backgroundView_nation.backgroundColor = WhiteColor;
    
    self.backgroundView_birthday = [UIView new];
    self.backgroundView_birthday.backgroundColor = WhiteColor;
    
    self.backgroundView_address = [UIView new];
    self.backgroundView_address.backgroundColor = WhiteColor;
    
    self.backgroundView_IDNumber = [UIView new];
    self.backgroundView_IDNumber.backgroundColor = WhiteColor;
    
    self.backgroundView_tradePassword = [UIView new];
    self.backgroundView_tradePassword.backgroundColor = WhiteColor;
    
    self.backgroundView_tradePasswordConfirm = [UIView new];
    self.backgroundView_tradePasswordConfirm.backgroundColor = WhiteColor;
    
    self.label_tip = [UILabel getLabWithText:@"请填写本人身份证信息!"
                                      textFont:Font_13
                                     textColor:RedColor
                                 textAlignment:NSTextAlignmentCenter
                                 numberOfLines:0];
    
    self.titleLabel_name     = [UILabel getLabWithText:@"姓       名:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_gender   = [UILabel getLabWithText:@"性       别:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_nation   = [UILabel getLabWithText:@"民       族:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_birthday = [UILabel getLabWithText:@"出生日期:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_address  = [UILabel getLabWithText:@"地       址:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_IDNumber = [UILabel getLabWithText:@"身份证号:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_tradePassword = [UILabel getLabWithText:@"交易密码:" textFont:Font_15 textColor:BlackColor];
    self.titleLabel_tradePasswordConfirm = [UILabel getLabWithText:@"确认密码:"  textFont:Font_15 textColor:BlackColor];
    
    
    self.textField_name                  = [[UITextField alloc] init];
    self.textField_name.font             = Font(15);
    self.textField_name.keyboardType     = UIKeyboardTypeDefault;
    self.textField_name.placeholder      = @"请输入姓名";
    self.textField_name.textColor        = [UIColor blackColor];
    self.textField_name.delegate         = self;
    self.textField_name.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_name.borderStyle      = UITextBorderStyleNone;
    [self.textField_name addTarget:self
                                   action:@selector(textFieldDidChange:)
                         forControlEvents:UIControlEventEditingChanged];
    
    self.textField_gender                  = [[UITextField alloc] init];
    self.textField_gender.font             = Font(15);
    self.textField_gender.keyboardType     = UIKeyboardTypeDefault;
    self.textField_gender.placeholder      = @"请输入性别";
    self.textField_gender.textColor        = [UIColor blackColor];
    self.textField_gender.delegate         = self;
    self.textField_gender.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_gender.borderStyle      = UITextBorderStyleNone;
    [self.textField_gender addTarget:self
                            action:@selector(textFieldDidChange:)
                  forControlEvents:UIControlEventEditingChanged];
    
    self.textField_nation                  = [[UITextField alloc] init];
    self.textField_nation.font             = Font(15);
    self.textField_nation.keyboardType     = UIKeyboardTypeDefault;
    self.textField_nation.placeholder      = @"请输入民族";
    self.textField_nation.textColor        = [UIColor blackColor];
    self.textField_nation.delegate         = self;
    self.textField_nation.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_nation.borderStyle      = UITextBorderStyleNone;
    [self.textField_nation addTarget:self
                            action:@selector(textFieldDidChange:)
                  forControlEvents:UIControlEventEditingChanged];
    
    self.textField_birthday                  = [[UITextField alloc] init];
    self.textField_birthday.font             = Font(15);
    self.textField_birthday.keyboardType     = UIKeyboardTypeDefault;
    self.textField_birthday.placeholder      = @"请输入出生日期";
    self.textField_birthday.textColor        = [UIColor blackColor];
    self.textField_birthday.delegate         = self;
    self.textField_birthday.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_birthday.borderStyle      = UITextBorderStyleNone;
    [self.textField_birthday addTarget:self
                            action:@selector(textFieldDidChange:)
                  forControlEvents:UIControlEventEditingChanged];
    
    self.textField_address                  = [[UITextField alloc] init];
    self.textField_address.font             = Font(15);
    self.textField_address.keyboardType     = UIKeyboardTypeDefault;
    self.textField_address.placeholder      = @"请输入地址";
    self.textField_address.textColor        = [UIColor blackColor];
    self.textField_address.delegate         = self;
    self.textField_address.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_address.borderStyle      = UITextBorderStyleNone;
    [self.textField_address addTarget:self
                            action:@selector(textFieldDidChange:)
                  forControlEvents:UIControlEventEditingChanged];
    
    self.textField_IDNumber                  = [[UITextField alloc] init];
    self.textField_IDNumber.font             = Font(15);
    self.textField_IDNumber.keyboardType     = UIKeyboardTypeNumbersAndPunctuation;
    self.textField_IDNumber.placeholder      = @"请输入身份证号";
    self.textField_IDNumber.textColor        = [UIColor blackColor];
    self.textField_IDNumber.delegate         = self;
    self.textField_IDNumber.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_IDNumber.borderStyle      = UITextBorderStyleNone;
    [self.textField_IDNumber addTarget:self
                           action:@selector(textFieldDidChange:)
                 forControlEvents:UIControlEventEditingChanged];
    
    
    self.textField_tradePassword                  = [[UITextField alloc] init];
    self.textField_tradePassword.font             = Font(15);
    self.textField_tradePassword.keyboardType     = UIKeyboardTypeNumberPad;
    self.textField_tradePassword.placeholder      = @"请输入6位数字交易密码";
    self.textField_tradePassword.textColor        = [UIColor blackColor];
    self.textField_tradePassword.delegate         = self;
    self.textField_tradePassword.secureTextEntry  = YES;
    self.textField_tradePassword.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_tradePassword.borderStyle      = UITextBorderStyleNone;
    [self.textField_tradePassword addTarget:self
                               action:@selector(textFieldDidChange:)
                     forControlEvents:UIControlEventEditingChanged];
    
    self.textField_tradePasswordConfirm                  = [[UITextField alloc] init];
    self.textField_tradePasswordConfirm.font             = Font(15);
    self.textField_tradePasswordConfirm.keyboardType     = UIKeyboardTypeNumberPad;
    self.textField_tradePasswordConfirm.placeholder      = @"确认交易密码";
    self.textField_tradePasswordConfirm.textColor        = [UIColor blackColor];
    self.textField_tradePasswordConfirm.delegate         = self;
    self.textField_tradePasswordConfirm.secureTextEntry  = YES;
    self.textField_tradePasswordConfirm.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.textField_tradePasswordConfirm.borderStyle      = UITextBorderStyleNone;
    [self.textField_tradePasswordConfirm addTarget:self
                           action:@selector(textFieldDidChange:)
                 forControlEvents:UIControlEventEditingChanged];
    
    self.button_confirm = [UIButton getClickBtnWithTitle:@"提交实名认证信息"];
    [self.button_confirm addTarget:self action:@selector(submitInfo:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self addSubview:self.backgroundScrollView];
    
    [self.backgroundScrollView addSubview:self.backgroundView_name];
    [self.backgroundScrollView addSubview:self.backgroundView_gender];
    [self.backgroundScrollView addSubview:self.backgroundView_nation];
    [self.backgroundScrollView addSubview:self.backgroundView_birthday];
    [self.backgroundScrollView addSubview:self.backgroundView_address];
    [self.backgroundScrollView addSubview:self.backgroundView_IDNumber];
    [self.backgroundScrollView addSubview:self.backgroundView_tradePassword];
    [self.backgroundScrollView addSubview:self.backgroundView_tradePasswordConfirm];
    
    [self.backgroundScrollView addSubview:self.label_tip];
    
    [self.backgroundScrollView addSubview:self.titleLabel_name];
    [self.backgroundScrollView addSubview:self.titleLabel_gender];
    [self.backgroundScrollView addSubview:self.titleLabel_nation];
    [self.backgroundScrollView addSubview:self.titleLabel_birthday];
    [self.backgroundScrollView addSubview:self.titleLabel_address];
    [self.backgroundScrollView addSubview:self.titleLabel_IDNumber];
    [self.backgroundScrollView addSubview:self.titleLabel_tradePassword];
    [self.backgroundScrollView addSubview:self.titleLabel_tradePasswordConfirm];
    
    [self.backgroundScrollView addSubview:self.textField_name];
    [self.backgroundScrollView addSubview:self.textField_gender];
    [self.backgroundScrollView addSubview:self.textField_nation];
    [self.backgroundScrollView addSubview:self.textField_birthday];
    [self.backgroundScrollView addSubview:self.textField_address];
    [self.backgroundScrollView addSubview:self.textField_IDNumber];
    [self.backgroundScrollView addSubview:self.textField_tradePassword];
    [self.backgroundScrollView addSubview:self.textField_tradePasswordConfirm];
    
    
    [self.backgroundScrollView addSubview:self.button_confirm];

    
    [self addConstraints];
}

- (void)addConstraints {
    
    [self.backgroundScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(Main_Width);
        make.height.mas_equalTo(Main_Height-64);
    }];
    
    [self.label_tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundScrollView).offset(10*Scale_H);
        make.left.mas_equalTo(self.backgroundScrollView).offset(20*Scale_W);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-20*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.label_tip.mas_bottom).offset(10*Scale_H);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_name);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_name.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_name);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_name.mas_bottom).offset(1);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_gender);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_gender.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_gender);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_nation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_gender.mas_bottom).offset(1);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_nation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_nation);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_nation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_nation.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_nation);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_birthday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_nation.mas_bottom).offset(1);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_birthday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_birthday);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_birthday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_birthday.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_birthday);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_birthday.mas_bottom).offset(1);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_address);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_address.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_address);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    
    /*****************************************************************************/
    [self.backgroundView_IDNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_address.mas_bottom).offset(1);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_IDNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_IDNumber);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_IDNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_IDNumber.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_IDNumber);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    /*****************************************************************************/
    [self.backgroundView_tradePassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_IDNumber.mas_bottom).offset(20*Scale_H);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_tradePassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_tradePassword);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_tradePassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel_tradePassword.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.titleLabel_tradePassword);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-40*Scale_W);
    }];
    
    
    /*****************************************************************************/
    [self.backgroundView_tradePasswordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_tradePassword.mas_bottom).offset(1*Scale_H);
        make.left.right.mas_equalTo(self.backgroundScrollView);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.titleLabel_tradePasswordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundScrollView).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.backgroundView_tradePasswordConfirm);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.textField_tradePasswordConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLabel_tradePasswordConfirm);
        make.left.right.mas_equalTo(self.textField_tradePassword);
    }];
    
    /*****************************************************************************/
    [self.button_confirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView_tradePasswordConfirm.mas_bottom).offset(20*Scale_H);
        make.left.mas_equalTo(self.backgroundScrollView).offset(20*Scale_W);
        make.right.mas_equalTo(self.backgroundScrollView).offset(-20*Scale_W);
        make.height.mas_equalTo(44*Scale_H);
    }];
    

    
}

@end
